package com.glorysoft.wangnianxinpda.activity;

import android.os.Handler;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.adapter.BatchAdapter;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityBatchStartWorkBinding;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

/**
 * FileName: BatchStartWorkActivity
 * Author: XJH
 * Date: 2021/12/21 14:58
 * Description:
 * History:
 */
public class BatchStartWorkActivity extends BaseActivity {
    private ActivityBatchStartWorkBinding mBinding;
    private BatchAdapter mAdapter;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_batch_start_work;
    }

    @Override
    protected void initData() {
        FreeApi_EditTextUtils.setEditTextListener(mBinding.etLot, this, s -> {
            for (String datum : mAdapter.getData()) {
                if (datum.equals(s)){
                    showToast("当前批次已添加到列表",false);
                    return;
                }
            }
            mAdapter.addData(s);
            mBinding.etLot.setText("");
            new Handler().postDelayed(() -> mBinding.etLot.requestFocus(),500);
        });
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_batch_start_work);

        mBinding.rvLotList.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvLotList.setItemAnimator(new DefaultItemAnimator());
        mBinding.rvLotList.addItemDecoration(new DividerItemDecoration(this, 1));

        mAdapter = new BatchAdapter(null);
        mBinding.rvLotList.setAdapter(mAdapter);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.batchStartWork));
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mBinding.etEqp.getText().toString().isEmpty()){
                    showToast("请扫描设备号",false);
                    return;
                }

                if (mAdapter.getData().size() <= 0){
                    showToast("请先添加批次信息",false);
                    return;
                }

                batchTrackIn();
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), mBinding.llMain);
    }

    private void batchTrackIn() {
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    showMessageDialog(response.body().getMsg(), getSupportFragmentManager());

                }
            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDATrackOut");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
        map.addProperty("EQPID", mBinding.etEqp.getText().toString());//设备号
        map.addProperty("RecipeName", mBinding.etRecipeName.getText().toString());//设备号
        StringBuilder sb = new StringBuilder();
        for (String datum : mAdapter.getData()) {
            sb.append(datum + ";");
        }
        map.addProperty("LOTID", sb.toString());//工单号
        String method = "PDAManyTrackIN";
        NetUtils.requestPostNet("PDAManyTrackIN", method, this, map, callBack);
    }
    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");

    }

}
